package 平时作业.代忠恒.BankSystem;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.util.HashMap;

public class BankSystem2 {
    HashMap<String, Save>bankdb;
    Scanner input;
    public BankSystem2() {
        bankdb = new HashMap<>();
        input = new Scanner(System.in);
    }
    public void initBankDB() throws Exception {

        File file=new File("");
        FileReader reader=new FileReader(file);

        BufferedReader br=new BufferedReader(reader);
        String str="";
        while((str= br.readLine())!=null){
            String[] save_arr=  str.split(",");
            Save save = new Save();
            save.setAccountid(save_arr[0]);
            save.setName(save_arr[1]);
            save.setPwd(save_arr[2]);
            save.setGender(save_arr[3]);
            save.setMoney(Double.parseDouble(save_arr[4]));
            bankdb.put(save_arr[0], save);
            Thread.sleep(1000);
            System.out.println(save);
        }
        System.out.println("账户初始化信息完毕");

    }
    public void runBankSystem() throws Exception{
        String flags = "N";
        do{
        BankSystemMenu.showMenu();
        String userInput=input.next();
        switch (userInput) {
            case "1" ->{
                createAccount();
            }
            case "2" ->{
                deleteAccount();
            }
            case "3" ->{
                saveMoney();
            }
            case "4" ->{
                getMoney();
            }
            case "5" ->{
                changeMoney();
            }
            case "6" ->{
                showBalance();
            }
            case "7" ->{

            }
            default ->{
                System.out.println("您输入有误！请重试！");
            }
        }
            System.out.println("是否继续操作本系统？(Y|N)");
            flags = input.next();
        }while(flags.equalsIgnoreCase("y"));
        System.out.println("欢迎下次再来！");
    }

    public void createAccount(){
        System.out.println("正在创建账户...");
        System.out.println("请输入用户名：");
        String name = input.next();
        System.out.println("请输入密码：");
        String pwd = input.next();
        System.out.println("请输入性别：");
        String gender = input.next();
        Save save = new Save();
        String accountid = "6226" + (int) (1000 + (Math.random()*9000));
        System.out.println("以为您生成一个专属卡号：" + accountid);
        save.setAccountid(accountid);
        save.setName(name);
        save.setPwd(pwd);
        save.setGender(gender);
        bankdb.put(accountid, save);
        System.out.println("您已开户完成");
        System.out.println("您的开户信息：" + bankdb);
    }
    public void saveMoney() throws Exception{
        boolean iscontinue = false;
        int count = 3;
        System.out.println("正在执行存款功能...");
        System.out.println("-----------------");
        System.out.println("请输入您的银行卡号：");
        String account = input.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd = input.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("请输入存款金额：");
                    double money = input.nextDouble();
                    save.setMoney(money+ save.getMoney());
                    bankdb.put(account,save);
                    System.out.println("存款成功");
                    System.err.printf("您的卡号：%s,您的姓名：%s,您的余额：%f\n", save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue = false;
                }
                else {
                    count--;
                    if(count==0){
                        System.out.println("您的密码次数已用尽，即将退出存款");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您的密码有误,还剩下"+count+"次机会，请重试！");
                    iscontinue = true;
                }
            }while(iscontinue);
        }
        else {
            System.out.println("您的银行号输入有误！");
        }

    }
    public void getMoney() throws Exception{
        boolean iscontinue=false;
        int count=3;
        System.out.println("正在执行取款");
        System.out.println("---------------");
        System.out.println("请输入银行卡号");
        String account=input.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd=input.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("请输入取款金额");
                    double money=input.nextDouble();
                    if(money> save.getMoney()){
                        System.out.println("余额不足");
                        return;
                    }else{
                        save.setMoney(save.getMoney()-money);
                        bankdb.put(account,save);
                    }
//                    save.setMoney(money+save.getMoney());
//                    bankdb.put(account,save);
                    System.out.println("取款成功");
                    System.err.printf("您的卡号%s:您的姓名%s,您的余额%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }
                else {
                    count--;
                    if (count==0) {
                        System.out.println("密码次数已经用完");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("密码有误,还剩下，"+count+"请重试");
                    iscontinue=true;
                }

            }while (iscontinue);
        }else {
            System.out.println("您输入银行卡号有误,请重试");
        }

    }
    public void changeMoney(){

    }
    public void showBalance() throws Exception{
        boolean iscontinue = false;
        int count = 3;
        System.out.println("正在执行查询余额...");
        System.out.println("-----------------");
        System.out.println("请输入您的银行卡号：");
        String account = input.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd = input.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("正在查询账户信息，请稍后....");
                    Thread.sleep(1000);
                    System.err.printf("您的卡号：%s,您的姓名：%s,您的余额：%f\n", save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue = false;
                }
                else {
                    count--;
                    if(count==0){
                        System.out.println("您的密码次数已用尽，即将退出查询功能");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您的密码有误,还剩下"+count+"次机会，请重试！");
                    iscontinue = true;
                }
            }while(iscontinue);
        }
        else {
            System.out.println("您的银行号输入有误！");
        }


    }
    public void deleteAccount() throws Exception{
        boolean iscontinue = false;
        int count = 3;
        System.out.println("正在执行销户...");
        System.out.println("-----------------");
        System.out.println("请输入您的银行卡号：");
        String account = input.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd = input.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("正在进行销户，请稍后....");
                    Thread.sleep(1000);
                    bankdb.remove(account);
                    System.out.println("销户完成");
                    iscontinue = false;
                }
                else {
                    count--;
                    if(count==0){
                        System.out.println("您的密码次数已用尽，即将退出销户功能");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您的密码有误,还剩下"+count+"次机会，请重试！");
                    iscontinue = true;
                }
            }while(iscontinue);
        }
        else {
            System.out.println("您的银行号输入有误！");
        }


    }
    public void exit(){
        System.out.println("是否真的退出系统？(Y|N):");
        String userInput = input.next();
        if(userInput.equalsIgnoreCase("y")){
            System.exit(0);
        }
    }
}
